package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.common.constant.LndxKTConstants;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.app.dto.LndxCourseDTO;
import org.springblade.modules.app.entity.LndxCourse;
import org.springblade.modules.app.entity.LndxFile;
import org.springblade.modules.app.service.LndxCourseService;
import org.springblade.modules.app.service.LndxFileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("blade-app/course")
public class LndxCourseController extends BladeController {

    @Autowired
    private LndxCourseService lndxCourseService;

    @Autowired
    private LndxFileService lndxFileService;


    @GetMapping("/pageBySearch")
    public R<IPage<LndxCourseDTO>> pageBySearch(Page page, LndxCourseDTO param) {
        LndxCourseDTO  authparam = new LndxCourseDTO ();
        if (!StringUtils.isEmpty(param.getCategory1())) {
            authparam.setCategory1(param.getCategory1());
        }
        if (!StringUtils.isEmpty(param.getCategory2())) {
            authparam.setCategory2(param.getCategory2());
        }
        if (!StringUtils.isEmpty(param.getCategory3())) {
            authparam.setCategory3(param.getCategory3());
        }
        if (!StringUtils.isEmpty(param.getCoursename())) {
            authparam.setCoursename(param.getCoursename());
        }

        IPage<LndxCourseDTO> pages = lndxCourseService.getLndxLndxCourseDTOPage(page,authparam);

        for (LndxCourseDTO entity: pages.getRecords()) {
            if(StringUtils.isEmpty(entity.getFiledic())){
                entity.setVideourl(entity.getFilehost()+entity.getFilepath()+"/"+entity.getFilename());
            }else{
                entity.setVideourl(entity.getFilehost()+entity.getFiledic()+"/"+entity.getFilepath()+"/"+entity.getFilename());
            }
        }
        return R.data(pages);
    }

    @PostMapping("/saveCourse")
    public R saveCourse(@RequestBody LndxCourseDTO lndxCourseDTO) {
        ///save course
        String  primaryKey= IdWorker.getIdStr();
        LndxCourse lndxCourse = new LndxCourse();
        BeanUtils.copyProperties(lndxCourseDTO,lndxCourse);
        lndxCourse.setId(primaryKey);
        lndxCourse.setFileid(primaryKey);
        boolean flag = lndxCourseService.save(lndxCourse);
        if(flag){
            LndxFile lndxFile = new LndxFile();
            lndxFile.setId(primaryKey);
            lndxFile.setFilehost(LndxKTConstants.FILE_HOST);
            SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMdd");
            lndxFile.setFilepath(sdf.format(new Date()));
            lndxFile.setFilename(lndxCourseDTO.getFilename());
            lndxFile.setCtime(new Date());
            boolean result = lndxFileService.save(lndxFile);
            return R.status(result);
        }else{
            return R.fail("保存失败");
        }

    }

}
